package cn.android.weblog;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.List;

import cn.android.weblog.adapter.PassageListAdapter;
import cn.android.weblog.api.CatagoryAPI;
import cn.android.weblog.api.PassageAPI;
import cn.android.weblog.domain.Catagory;
import cn.android.weblog.domain.Passage;
import cn.android.weblog.redisdata.LOGIN;

public class WritePassageActivity extends AppCompatActivity {

    private Button cancel,commit;
    private ImageView back;
    private EditText title,describe,catagory,content,originalPassageId;
    private RadioGroup isDraft,isOriginal,isPrivate;
    private RadioButton is_create,is_original,is_draft,is_true,is_private,is_public;
    private PopupMenu pm;
    private CatagoryAPI catagoryAPI;
    private PassageAPI passageAPI;
    private Passage passage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write_passage);

        cancel=(Button)findViewById(R.id.write_cancel);
        commit=(Button)findViewById(R.id.write_add);
        back=(ImageView)findViewById(R.id.write_back);
        title=(EditText)findViewById(R.id.write_title);
        describe=(EditText)findViewById(R.id.write_describe);
        content=(EditText)findViewById(R.id.write_content);
        catagory=(EditText)findViewById(R.id.write_catagory);
        originalPassageId=(EditText)findViewById(R.id.write_href);
        isDraft=(RadioGroup)findViewById(R.id.write_rg2);
        isOriginal=(RadioGroup)findViewById(R.id.write_rg1);
        isPrivate=(RadioGroup)findViewById(R.id.write_rg3);
        is_create=(RadioButton)findViewById(R.id.is_create);
        is_original=(RadioButton)findViewById(R.id.is_turn);
        is_draft=(RadioButton)findViewById(R.id.is_draft);
        is_true=(RadioButton)findViewById(R.id.is_true);
        is_private=(RadioButton)findViewById(R.id.is_private);
        is_public=(RadioButton)findViewById(R.id.is_public);
        catagoryAPI=new CatagoryAPI();
        passageAPI=new PassageAPI();
        passage=new Passage();
        passage.setIsDraft(0);
        passage.setIsOriginal(0);
        passage.setIsPrivate(1);
        initMenu();

        // 设置单选框更换时，监听
        isDraft.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                changeSelect(group,checkedId);
            }
        });
        isOriginal.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                changeSelect(group,checkedId);
            }
        });
        isPrivate.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                changeSelect(group,checkedId);
            }
        });

        // 加载分类
        catagory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pm.show();
            }
        });

        // 取消:返回上一页
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 返回上一页
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 添加文章
        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addPassage();
            }
        });
    }


    /**
     * 添加文章
     */
    private void addPassage(){
        if (!getMessage()){
            Toast.makeText(this,"请输入完整的文章信息！",Toast.LENGTH_LONG).show();
            return;
        }
        String msg = passageAPI.add(passage, LOGIN.USER.getUserId());
        Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
        if (msg.equals("添加成功")){
            finish();
        }
    }

    private void changeSelect(RadioGroup group, int checkedId){
        switch (group.getCheckedRadioButtonId()){
            case R.id.is_create:passage.setIsOriginal(0);break;
            case R.id.is_turn:passage.setIsOriginal(1);break;
            case R.id.is_true:passage.setIsDraft(0);break;
            case R.id.is_draft:passage.setIsDraft(1);break;
            case R.id.is_private:passage.setIsPrivate(1);break;
            case R.id.is_public:passage.setIsPrivate(0);break;
        }
    }

    /**
     * 获取用户输入的文章信息
     */
    private boolean getMessage(){
        passage.setTitle(title.getText().toString().trim());
        passage.setDescribed(describe.getText().toString().trim());
        passage.setCatagoryName(catagory.getText().toString());
        passage.setContent("<p>"+content.getText().toString()+"</p>");
        if(passage.getTitle().equals("") || passage.getDescribed().equals("")
                || passage.getCatagoryName().equals("") || passage.getCatagoryId().equals("")
                || passage.getContent().equals("")) {
            return false;
        }
        // 选择转载的时候，必须输入原文链接
        if(is_original.isChecked() && originalPassageId.equals("")) {
            return false;
        }
        // 单选框的值在每次改变的时候都修改了passage中的值，这里无需重新获取
        // 当选择转载时，获取输入链接
        if (passage.getIsOriginal() == 1){
            passage.setOriginalPassageId(originalPassageId.getText().toString().trim());
        }
        return true;
    }

    /**
     * 初始化我的分类下拉框
     */
    private void initMenu(){
        pm=new PopupMenu(WritePassageActivity.this,title);
        Menu menu=pm.getMenu();
        pm.getMenuInflater().inflate(R.menu.write_catagory,menu);
        List<Catagory> lists = catagoryAPI.findCatagoryByUserId(LOGIN.USER.getUserId());
        // 将分类全部添加到菜单项中
        for(Catagory c:lists){
            // 1-组别、2-菜单项id、3-菜单项顺序、4-菜单项内容
            menu.add(0,c.getCatagoryId(),0,c.getCatagoryName());
        }
        // 添加单击数据项事件
        pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                // 当选择某一个分类的时候,显示在编辑框上
                catagory.setText(item.getTitle());
                passage.setCatagoryId(item.getItemId());
                return true;
            }
        });
    }

}
